package com.xcdh.orm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xcdh.commons.utils.exception.AssertUtil;
import com.xcdh.domain.RequestPage;
import com.xcdh.orm.ddl.DDLIndex;
import com.xcdh.orm.ddl.DDLTable;
import com.xcdh.orm.ddl.DDLTableColumn;
import com.xcdh.orm.handler.DbServiceProxy;
import com.xcdh.orm.service.TableDocService;
import com.xcdh.orm.service.ddl.DDLColumnService;
import com.xcdh.orm.service.ddl.DDLIndexService;
import com.xcdh.orm.service.ddl.DDLTableService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TableDocServiceImpl
 * Created by wjg on 2023/12/5.
 */
@Service
public class TableDocServiceImpl implements TableDocService {
    @Override
    public List<DDLTable> tableDoc(String serviceId, String dataBase) {
        DDLTable table = new DDLTable() ;
        table.setDataBaseName(dataBase);
        List<DDLTable> ddlTableList = DbServiceProxy.cretedDDLService(serviceId, DDLTableService.class).queryTables(table);
        DDLColumnService ddlColumnService = DbServiceProxy.cretedDDLService(serviceId, DDLColumnService.class);
        for (DDLTable ddlTable : ddlTableList) {
            List<DDLTableColumn> tableColumns = ddlColumnService.getTableColumns(ddlTable);
            ddlTable.setColumns(tableColumns);
        }
        return ddlTableList;
    }

    @Override
    public List<DDLTable> tableAllDoc(String serviceId,String dataBase) {
        DDLTable table = new DDLTable() ;
        table.setDataBaseName(dataBase);
        List<DDLTable> ddlTableList = DbServiceProxy.cretedDDLService(serviceId, DDLTableService.class).queryTables(table);
        DDLColumnService ddlColumnService = DbServiceProxy.cretedDDLService(serviceId, DDLColumnService.class);
        DDLIndexService ddlIndexService = DbServiceProxy.cretedDDLService(serviceId, DDLIndexService.class);
        for (DDLTable ddlTable : ddlTableList) {
            List<DDLTableColumn> tableColumns = ddlColumnService.getTableColumns(ddlTable);
            DDLIndex index = new DDLIndex(ddlTable.getTableName() , dataBase) ;
            List<DDLIndex> indices = ddlIndexService.queryIndex(index);
            ddlTable.setColumns(tableColumns);
            ddlTable.setIdxList(indices);
        }
        return ddlTableList;
    }

    @Override
    public List<DDLTable> tableAllDoc(String serviceId, String tableName, String dataBase) {
        DDLTable table = new DDLTable() ;
        table.setDataBaseName(dataBase);
        table.setTableName(tableName);
        List<DDLTable> ddlTableList = DbServiceProxy.cretedDDLService(serviceId, DDLTableService.class).queryTables(table);
        DDLColumnService ddlColumnService = DbServiceProxy.cretedDDLService(serviceId, DDLColumnService.class);
        DDLIndexService ddlIndexService = DbServiceProxy.cretedDDLService(serviceId, DDLIndexService.class);
        for (DDLTable ddlTable : ddlTableList) {
            List<DDLTableColumn> tableColumns = ddlColumnService.getTableColumns(ddlTable);
            DDLIndex index = new DDLIndex(ddlTable.getTableName() , dataBase) ;
            List<DDLIndex> indices = ddlIndexService.queryIndex(index);
            ddlTable.setColumns(tableColumns);
            ddlTable.setIdxList(indices);
        }
        return ddlTableList;
    }

    @Override
    public IPage<DDLTable> pageTableDoc(String serviceId, RequestPage<DDLTable> query) {
        AssertUtil.blankError(query.getModel().getDataBaseName() , "数据库不可为空");
        IPage<DDLTable> ddlTableIPage = DbServiceProxy.cretedDDLService(serviceId, DDLTableService.class).pageTable(query);
        DDLColumnService ddlColumnService = DbServiceProxy.cretedDDLService(serviceId, DDLColumnService.class);
        for (DDLTable ddlTable : ddlTableIPage.getRecords()) {
            List<DDLTableColumn> tableColumns = ddlColumnService.getTableColumns(ddlTable);
            ddlTable.setColumns(tableColumns);
        }
        return ddlTableIPage;
    }
}
